﻿namespace IQIGame.Onigao.Logic
{
    public class BuffEffective_Morph : BuffEffective
    {
        protected override void OnInitProperty(BuffEffectData rData, BuffComponent rTarget)
        {
            base.OnInitProperty(rData, rTarget);
            var rConfigData = new ConfigData();
            if (rData.paramStr.Count > 0)
            {
                rConfigData.resName = rData.paramStr[0];
            }
            if (rData.param.Count > 0)
            {
                rConfigData.resType = rData.param[0];
            }
            rData.userData = rConfigData;
        }

        protected override void OnActive(BuffEffectContext rContext, BuffEffectData rConfig, int nRuntimeParam)
        {
            base.OnActive(rContext, rConfig, nRuntimeParam);
            if (rConfig.userData is ConfigData rConfigData)
            {
                var rTarget = rContext.buff.target;
                var rPOD = BattleUpdateFactory.CreateMorph(rTarget.unit, rConfigData.resName, rConfigData.resType, true);
                rTarget.unit.battle.playCtrl.resultCtrl.Add(rPOD);
            }
        }

        protected override void OnDeActive(BuffEffectContext rContext, BuffEffectData rConfig)
        {
            base.OnDeActive(rContext, rConfig);
            if (rConfig.userData is ConfigData rConfigData)
            {
                var rTarget = rContext.buff.target;
                var rPOD = BattleUpdateFactory.CreateMorph(rTarget.unit, rConfigData.resName, rConfigData.resType, false);
                rTarget.unit.battle.playCtrl.resultCtrl.Add(rPOD);
            }
        }

        private class ConfigData : BuffEffectiveConfig
        {
            public string resName;
            public int resType;
        }

#if UNITY_EDITOR
        public override string ToString(BuffEffectData rConfig)
        {
            return $"变身[{(rConfig.userData as ConfigData)?.resName}]";
        }
#endif
    }
}